//
//  WCIncludeDirectory.m
//  WabbitCode
//
//  Created by William Towe on 1/30/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCIncludeDirectory.h"
#import "WCProject.h"
#import "NSImage+WCExtensions.h"
#import "NSDictionary+WCExtensions.h"
#import "WCFileReference.h"


static NSFileManager *fm = nil;

#define kWCIDFileReferenceKey @"fileReference"
#define kWCIDBuildConfigurationKey @"buildConfiguration"

@implementation WCIncludeDirectory
#pragma mark *** Subclass Overrides ***
+ (void)initialize {
	fm = [NSFileManager defaultManager];
}

- (void)dealloc {
	[_fileReference release];
	[super dealloc];
}

- (BOOL)isLeaf {
	return YES;
}

- (NSImage *)icon {
	return [[self fileReference] icon];
}

- (NSString *)nodeToolTip {
	return [self directoryPath];
}

- (BOOL)canRenameNode {
	return NO;
}

- (BOOL)isValid {
	BOOL isDirectory = NO;
	return ([fm fileExistsAtPath:[self directoryPath] isDirectory:&isDirectory] && isDirectory)?YES:NO;
}

#pragma mark NSCoding Protocol
- (id)initWithCoder:(NSCoder *)coder {
	if (!(self = [super init]))
		return nil;
	
	_fileReference = [[coder decodeObjectForKey:kWCIDFileReferenceKey] retain];
	_buildConfiguration = [coder decodeObjectForKey:kWCIDBuildConfigurationKey];
	
	return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
	[coder encodeObject:_fileReference forKey:kWCIDFileReferenceKey];
	[coder encodeConditionalObject:_buildConfiguration forKey:kWCIDBuildConfigurationKey];
}
#pragma mark NSCopying Protocol
- (id)copyWithZone:(NSZone *)zone {
	WCIncludeDirectory *copy = [[[self class] alloc] init];
	
	copy->_fileReference = [_fileReference copy];
	copy->_buildConfiguration = _buildConfiguration;
	
	return copy;
}
#pragma mark NSMutableCopying Protocol
- (id)mutableCopyWithZone:(NSZone *)zone {
	WCIncludeDirectory *copy = [[[self class] alloc] init];
	
	copy->_fileReference = [_fileReference mutableCopy];
	copy->_buildConfiguration = _buildConfiguration;
	
	return copy;
}
#pragma mark *** Public Methods ***
#pragma mark Accessors
@synthesize fileReference=_fileReference,buildConfiguration=_buildConfiguration;

@dynamic directoryPath;
- (NSString *)directoryPath {
	return [[self fileReference] path];
}

@dynamic name;
- (NSString *)name {
	return [[self directoryPath] lastPathComponent];
}

@dynamic directoryPathByAbbreviatingWithTildeInPath;
- (NSString *)directoryPathByAbbreviatingWithTildeInPath {
	return [[self directoryPath] stringByAbbreviatingWithTildeInPath];
}
#pragma mark Creation
+ (id)includeDirectoryWithDirectoryPath:(NSString *)directoryPath forBuildConfiguration:(WCBuildConfiguration *)buildConfiguration; {
	return [[[[self class] alloc] initWithDirectoryPath:directoryPath forBuildConfiguration:buildConfiguration] autorelease];
}

- (id)initWithDirectoryPath:(NSString *)directoryPath forBuildConfiguration:(WCBuildConfiguration *)buildConfiguration; {
	if (!(self = [super init]))
		return nil;
	
	_fileReference = [[WCFileReference alloc] initWithFilePath:directoryPath];
	_buildConfiguration = buildConfiguration;
	
	return self;
}

@end
